package com.project.web.controller.system;

import java.util.List;

import com.project.common.utils.file.FileUploadUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.system.domain.GoodsInfo;
import com.project.system.service.IGoodsInfoService;
import com.project.web.controller.common.BaseController;
import com.project.common.base.TableDataInfo;
import com.project.common.base.AjaxResult;
import org.springframework.web.multipart.MultipartFile;

/**
 * 餐饮 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/system/goodsInfo")
public class GoodsInfoController extends BaseController {
    private String prefix = "system/goodsInfo";

    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 跳转页面
     *
     * @return
     */
    @RequiresPermissions("system:goodsInfo:view")
    @GetMapping()
    public String goodsInfo() {
        return prefix + "/goodsInfo";
    }

    /**
     * 查询餐饮列表
     *
     * @param goodsInfo
     * @return
     */
    @RequiresPermissions("system:goodsInfo:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsInfo goodsInfo) {
        if (!this.getUser().isSuperAdmin()) {
            goodsInfo.setUserId(this.getUserId());
        }
        goodsInfo.getParams().put("getAll", "1");
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }


    /**
     * 跳转新增餐饮页面
     *
     * @return
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存餐饮
     *
     * @param goodsInfo
     * @param file
     * @return
     * @throws Exception
     */
    @RequiresPermissions("system:goodsInfo:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsInfo goodsInfo, MultipartFile file) throws Exception {
        goodsInfo.setUserId(this.getUserId());
        goodsInfo.setImgUrl(FileUploadUtils.upload(file, null));
        return toAjax(goodsInfoService.insertGoodsInfo(goodsInfo));
    }

    /**
     * 查看餐饮详情
     *
     * @param goodsId
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{goodsId}")
    public String detail(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(goodsId);
        mmap.put("goodsInfo", goodsInfo);
        return prefix + "/detail";
    }

    /**
     * 修改餐饮页面
     *
     * @param goodsId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") Long goodsId, ModelMap mmap) {
        GoodsInfo goodsInfo = goodsInfoService.selectGoodsInfoById(goodsId);
        mmap.put("goodsInfo", goodsInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存餐饮
     *
     * @param goodsInfo
     * @param file
     * @return
     * @throws Exception
     */
    @RequiresPermissions("system:goodsInfo:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsInfo goodsInfo, MultipartFile file) throws Exception {
        if (file != null) {
            goodsInfo.setImgUrl(FileUploadUtils.upload(file, null));
        }
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo));
    }

    /**
     * 删除餐饮
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("system:goodsInfo:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsInfoService.deleteGoodsInfoByIds(ids));
    }

}
